﻿using System.Collections.Generic;
using System.Security.Policy;
using System.Web.Mvc;

namespace CNav.Menu
{
    public static class HtmlHelperSiteMenu
    {
        public static MvcHtmlString ActionImage(this HtmlHelper html, string action, string controller, object routeValues, string imagePath, string alt)
        {
            var url = new UrlHelper(html.ViewContext.RequestContext);

            // build the <img> tag
            var imgBuilder = new TagBuilder("img");
            imgBuilder.MergeAttribute("src", url.Content(imagePath));
            imgBuilder.MergeAttribute("alt", alt);
            string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);

            // build the <a> tag
            var anchorBuilder = new TagBuilder("a");

            if (controller == null)
                anchorBuilder.MergeAttribute("href", url.Action(action, routeValues));
            else
                anchorBuilder.MergeAttribute("href", url.Action(action, controller, routeValues));

            anchorBuilder.InnerHtml = imgHtml; // include the <img> tag inside
            string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);

            return MvcHtmlString.Create(anchorHtml);
        }

        public static MvcHtmlString ActionImage(this HtmlHelper html, string action, object routeValues, string imagePath, string alt)
        {
            return ActionImage(html, action, null, routeValues, imagePath, alt);
        }

        public static MvcHtmlString SiteMenuAsUnorderedList(this HtmlHelper helper, List<ISiteLink> siteLinks)
        {
            if (siteLinks == null || siteLinks.Count == 0)
                return MvcHtmlString.Empty;
            var topLevelParentId = SiteLinkListHelper.GetTopLevelParentId(siteLinks);

            return MvcHtmlString.Create(BuildMenuItems(siteLinks, topLevelParentId));
        }

        private static string BuildMenuItems(List<ISiteLink> siteLinks, int parentId)
        {
            var parentTag = new TagBuilder("ul");

            var childSiteLinks = SiteLinkListHelper.GetChildSiteLinks(siteLinks, parentId);
            foreach (var siteLink in childSiteLinks)
            {
                var itemTag = new TagBuilder("li");

                if(siteLink.Selected)
                    itemTag.MergeAttribute("class", "selected");

                var anchorTag = new TagBuilder("a");
                anchorTag.MergeAttribute("href", siteLink.Url);
                anchorTag.SetInnerText(siteLink.Text);
                if (siteLink.OpenInNewWindow)
                {
                    anchorTag.MergeAttribute("target", "_blank");
                }
                itemTag.InnerHtml = anchorTag.ToString();
                if (SiteLinkListHelper.SiteLinkHasChildren(siteLinks, siteLink.Id))
                {
                    itemTag.InnerHtml += BuildMenuItems(siteLinks, siteLink.Id);
                }
                parentTag.InnerHtml += itemTag;
            }
            return parentTag.ToString();
        }
    }
}